/*
 * Copyright 2003-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.tsinghua.cic.ccss.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.forten.mail.template.FreeMarkerBean;
import org.forten.utils.common.NumberUtil;
import org.forten.utils.system.LogUtil;
import org.forten.utils.system.PropertiesFileReader;
import org.springframework.stereotype.Component;
import org.thcic.sms.client.SmsClient;

import cn.edu.tsinghua.cic.ccss.bo.CourseBo;
import cn.edu.tsinghua.cic.ccss.dto.SendMessageToCadreDto;

/**
 * 
 * 
 * @author <a href="mailto:du_yi@bbn.cn">Duyi</a>
 * @since 2012-5-23
 */
@Component("sendShortMessageToCadreTask")
public class SendShortMessageToCadreTask {
	private static Logger log = Logger
			.getLogger(SendShortMessageToCadreTask.class);

	@Resource
	private CourseBo courseBo;
	@Resource
	private FreeMarkerBean freeMarkerBean;

	private String url;
	private String appId;
	private String appKey;
	@Resource
	private EhcacheUtil ehcacheUtil;

	public SendShortMessageToCadreTask() {
		super();
		this.url = PropertiesFileReader.getValue("forten-system", "sms.url");
		this.appId = PropertiesFileReader
				.getValue("forten-system", "sms.appId");
		this.appKey = PropertiesFileReader.getValue("forten-system",
				"sms.appKey");
	}

	public void sendShortMessage() {
		try {
			Thread.sleep(NumberUtil.getRandomInteger(1, 30000));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		LogUtil.info(log, "shortMsgFlag is : %d",
				ehcacheUtil.getFlag("shortMsgFlag"));
		if (ehcacheUtil.getFlag("shortMsgFlag") != 0) {
			// ehcacheUtil.setFlag("shortMsgFlag", 0);
			return;
		} else {
			ehcacheUtil.setFlag("shortMsgFlag", 1);
			String message = "";
			LogUtil.info(log, "准备执行向干部发送开课通知短信任务");
			List<SendMessageToCadreDto> list = courseBo
					.querySendShortMessageToCadreDto();
			for (SendMessageToCadreDto dto : list) {
				Map<String, SendMessageToCadreDto> data = new HashMap<String, SendMessageToCadreDto>();
				data.put("dto", dto);
				LogUtil.info(log,"收到短信的手机号码=================:"+dto.getCadreMobileNums());
				String msg = freeMarkerBean.getTemplateContent(
						"to_students_sms.ftl", data);
				SmsClient.setServiceUrl(url);
				int code = SmsClient.sendMass(appId, appKey,
						dto.getCadreMobileNums(), msg);
				switch (code) {
				case 0:
					message = "SMS[0]:发送开课通知短信成功，success";
					break;
				case 10:
					message = "SMS[10]:调用参数错误，invokation param error";
					break;
				case 20:
					message = "SMS[20]:应用认证失败， auth error";
					break;
				case 900:
					message = "SMS[900]:数据库访问错误，DB access error";
					break;
				case 901:
					message = "SMS[901]:没有设置服务器URL地址，not setting server url address";
					break;
				case 902:
					message = "SMS[902]:连接服务器错误，server connection exception";
					break;
				default:
					break;
				}
				LogUtil.info(log, message);
			}
		}
	}
}
